package com.jyxy.service.web;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jyxy.service.model.CCompanyPatent;
import com.jyxy.service.util.StringUtil;
import com.jyxy.service.web.base.BaseController;

public class EntService extends BaseController{
	public void patentList() {
		String name = this.getPara("name");
		String ipc = this.getPara("ipc");
		String type = this.getPara("type");
		int pageSize = this.getParaToInt("page_size",10);
		String where = " 1=1 ";
		if(StringUtil.isNotNullStr(name)) {
			where += "and name like '%"+name+"%'";
		}
		if(StringUtil.isNotNullStr(ipc)) {
			where += "and ipc like '%"+ipc+"%'";
		}
		if(StringUtil.isNotNullStr(type)) {
			where += "and type like '%"+type+"%'";
		}
		Map<String,Object> reMap = new HashMap<>();
		int pageNum = this.getParaToInt("page_num",1);
		Page<CCompanyPatent> page = CCompanyPatent.dao.paginate(pageNum, pageSize, "SELECT id,`name`,patent_id,apply_date,public_date,type,legal_status,ipc"," FROM zsj_c_company_patent WHERE id in (SELECT id FROM zsj_c_company_patent where" + where+")");
		reMap.put("total", page.getTotalRow());
		reMap.put("data", page.getList());
		rendSuccess(reMap);
	}
	public void jobList() {
		int pageSize = this.getParaToInt("page_size",10);
		Map<String,Object> reMap = new HashMap<>();
		int pageNum = this.getParaToInt("page_num",1);
		Page<CCompanyPatent> page = CCompanyPatent.dao.paginate(pageNum, pageSize, "SELECT *" ,"FROM zsj_c_company_job ORDER BY salary,real_pub_date");
		reMap.put("total", page.getTotalRow());
		reMap.put("data", page.getList());
		rendSuccess(reMap);
	}
	public void patentDetail() {
		String id = this.getPara("id");
		CCompanyPatent patent = CCompanyPatent.dao.findById(id);
		rendSuccess(patent);
	}
	public void countService() {
		Map<String,Object> sqlmap = new HashMap<>();
		long count = Db.queryLong("SELECT COUNT(1) FROM `sys_api_log`");
		Long percent = Db.queryLong("SELECT floor(((SELECT COUNT(1) FROM `sys_api_log` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 7 DAY) AND curdate())-(SELECT COUNT(1) FROM `sys_api_log` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY)))/(SELECT COUNT(1) FROM `sys_api_log` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY))*100)");
		sqlmap.put("count",count);
		sqlmap.put("percent", percent==null?0:percent);
		rendSuccess(sqlmap);
	}
	public void countEnt() {
		Map<String,Object> sqlmap = new HashMap<>();
		long count = Db.queryLong("SELECT COUNT(1) FROM `zsj_c_company`");
		Long percent = Db.queryLong("SELECT floor (((SELECT SUM(day_increment) FROM zsj_table_insert_record WHERE  table_name='zsj_c_company' AND record_date BETWEEN date_sub(curdate(), INTERVAL 7 DAY) AND curdate())-(SELECT SUM(day_increment) FROM zsj_table_insert_record WHERE  table_name='zsj_c_company' AND record_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY)))/(SELECT SUM(day_increment) FROM zsj_table_insert_record WHERE  table_name='zsj_c_company' AND record_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY))*100)");
		sqlmap.put("count",count);
		sqlmap.put("percent", percent==null?0:percent);
		rendSuccess(sqlmap);
	}
	public void countDiskSize() {
		Map<String,Object> sqlmap = new HashMap<>();
		long count = Db.queryLong("SELECT SUM(size) FROM `zsj_linux_size` WHERE create_date=(SELECT MAX(create_date) FROM `zsj_linux_size`)");
		Long percent = Db.queryLong("SELECT floor (((SELECT COUNT(1) FROM `zsj_linux_size` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 7 DAY) AND curdate())-(SELECT COUNT(1) FROM `zsj_linux_size` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY)))/(SELECT COUNT(1) FROM `zsj_linux_size` WHERE create_date BETWEEN date_sub(curdate(), INTERVAL 14 DAY) AND date_sub(curdate(), INTERVAL 7 DAY))*100)");
		sqlmap.put("count",count);
		sqlmap.put("percent", percent==null?0:percent);
		rendSuccess(sqlmap);
	}
}
